package cn.iocoder.yudao.module.call.controller.admin.billrecord;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.call.controller.admin.billrecord.vo.BillRecordPageReqVO;
import cn.iocoder.yudao.module.call.controller.admin.billrecord.vo.BillRecordRespVO;
import cn.iocoder.yudao.module.call.dal.dataobject.billrecord.BillRecordDO;
import cn.iocoder.yudao.module.call.service.billrecord.BillRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 消费记录")
@RestController
@RequestMapping("/call/bill-record")
@Validated
public class BillRecordController {

    @Resource
    private BillRecordService billRecordService;


    @GetMapping("/page")
    @Operation(summary = "获得消费记录分页")
    @PreAuthorize("@ss.hasPermission('call:bill-record:query')")
    public CommonResult<PageResult<BillRecordRespVO>> getBillRecordPage(@Valid BillRecordPageReqVO pageReqVO) {
        PageResult<BillRecordDO> pageResult = billRecordService.getBillRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BillRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出消费记录 Excel")
    @PreAuthorize("@ss.hasPermission('call:bill-record:export')")
    @OperateLog(type = EXPORT)
    public void exportBillRecordExcel(@Valid BillRecordPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BillRecordDO> list = billRecordService.getBillRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "消费记录.xls", "数据", BillRecordRespVO.class,
                BeanUtils.toBean(list, BillRecordRespVO.class));
    }

}